package org.neuedu.servlet;

import org.neuedu.bean.User;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserSetServlet",urlPatterns = "/user/set")
public class UserSetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String nickname = request.getParameter("username");
        String gender = request.getParameter("sex");
        String city = request.getParameter("city");
        String sign = request.getParameter("sign");
        HttpSession session = request.getSession();
        User fly_user = (User)session.getAttribute("fly_user");
        Integer uid = fly_user.getId();

        //封装
        User user = new User();
        user.setId(uid);
        user.setNickName(nickname);
        user.setGender(Boolean.valueOf(gender));
        user.setCity(city);
        user.setSign(sign);

        UserService service = new UserServiceImpl();
        User userById = service.getUserById(uid);
        String sign1 = userById.getSign();
        request.setAttribute("sign",sign1);
        int i = service.userInfoUpdate(user);
        if( i != 0){
            //修改成功
            fly_user.setId(user.getId());
            fly_user.setCity(user.getCity());
            fly_user.setNickName(user.getNickName());
            fly_user.setSign(user.getSign());
            fly_user.setGender(user.getGender());
            request.setAttribute("msg","修改成功");

            request.getRequestDispatcher("/WEB-INF/jsp/user/set.jsp").forward(request, response);
        }else {
            //失败
            request.setAttribute("msg","修改失败");
            request.getRequestDispatcher("/WEB-INF/jsp/user/set.jsp").forward(request, response);
        }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
